package org.dsapr;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class ProgressBar {
	private static final int TOTAL = 100;
	private static final char BACK_GROUND = '░';
	private static final char FRONT_GROUND = '█';

	public static void main(String[] args) throws InterruptedException {

		StringBuilder stringBuilder = new StringBuilder();
		Stream.generate(() -> BACK_GROUND).limit(TOTAL).forEach(stringBuilder::append);

		for (int i = 0; i < TOTAL; i++) {
			stringBuilder.replace(i, i + 1, String.valueOf(FRONT_GROUND));
			String bar = "\r" + stringBuilder;
			String percent = " " + (1 + i) + "%";

			// 输出绿色进度条
			System.out.print(ProgressBar.ColorEnum.GREEN.getValue() + bar + percent);

			// 防止打印太快，方便观察
			TimeUnit.MILLISECONDS.sleep(i * 10L);
		}
	}

	/**
	 * 颜色枚举
	 */
	enum ColorEnum {

		/**
		 * 白色
		 */
		WHITE("\33[0m"),

		/**
		 * 红色
		 */
		RED("\33[1m\33[31m"),

		/**
		 * 绿色
		 */
		GREEN("\33[1m\33[32m"),

		/**
		 * 黄色
		 */
		YELLOW("\33[1m\33[33m"),

		/**
		 * 蓝色
		 */
		BLUE("\33[1m\33[34m"),

		/**
		 * 粉色
		 */
		PINK("\33[1m\33[35m"),

		/**
		 * 青色
		 */
		CYAN("\33[1m\33[36m");

		/**
		 * 颜色值
		 */
		private String value;

		ColorEnum(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}

	}

}
